﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using Kugar.Core.ExtMethod;
using Kugar.Core.Web;
using Kugar.Web.Wechat.Entities;
using Kugar.Web.Wechat.Enums;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using ZXing.Aztec.Internal;

namespace Kugar.Web.Wechat.NetCore.Test.Controllers
{
    public class LoginController : Controller
    {
        public async Task<IActionResult> Index()
        {
            var _defaultUser = new WechatUserOAuth()
            {
                OpenID = "sdsfdfsf",
                NickName = "test",
                Scope = WechatOAuthScope.snsapi_userinfo
            };

            var claims = new Claim[]
            {
                new Claim("wechat_openID", _defaultUser.OpenID.ToStringEx()),
                new Claim("wechat_appID", "sdfsdfsd"),
                new Claim("wechat_scope", ((int)_defaultUser.Scope).ToStringEx()),
            };

            var adminlteIdentity = new ClaimsIdentity(claims, "wechat");

            var identityPrincipal = new ClaimsPrincipal(adminlteIdentity);

            await HttpContext.SignInAsync("wechat", identityPrincipal, new AuthenticationProperties()
            {
                //AllowRefresh = true,
                IsPersistent = true,
                ExpiresUtc = DateTimeOffset.Now.AddDays(5)
            });

            return Redirect("/");

            return View();
        }

        public async Task<IActionResult> Logout()
        {
            await HttpContext.SignOutAsync("wechat");
            return Ok();
        }
    }
}